package com.example.yiwei.crm.view.activity.JYCoordingActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetCoorVisitData;
import com.example.yiwei.crm.data.GetVisit;
import com.example.yiwei.crm.entity.CoorSeacherVisitActivity;
import com.example.yiwei.crm.entity.scheduleEntity;
import com.example.yiwei.crm.json.VisitJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.Class.listviewClass.AutoListView;
import com.example.yiwei.crm.view.activity.coordinationActivity.CoordinationVisitActivitu;
import com.example.yiwei.crm.view.activity.healthActivity.detailedVisitActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class JYCoordVisitActivity extends BaseActivity implements AutoListView.OnRefreshListener,AutoListView.OnLoadListener{
    private AutoListView listView;
    private scheduleEntity scheduleEntity;
    int page = 1;
    private ImageView img_lr,img_cx,img_tj,img_me;
    int a = 1;
    List<scheduleEntity> list;
    List<scheduleEntity> alist;
    MyAdapter adapter ;
    private int dock_id;
    SharedPreferences sp;
    ImageView fabIconStar;
    ImageView lcIcon1;
    FloatingActionMenu leftCenterMenu;
    //    FloatingActionMenu leftCenterMenu1;
    FloatingActionButton leftCenterButton;
    private ImageView img_shouye,img_more1,img_back;
    private PopupWindow mPopWindow;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            List<scheduleEntity> result = (List<scheduleEntity>) msg.obj;
            switch (msg.what) {
                case AutoListView.REFRESH:
                    listView.onRefreshComplete();
                    if (list!=null){
                        list.clear();
                    }
                    list.addAll(result);
                    if (a==1){
                        adapter = new MyAdapter();
                        listView.setAdapter(adapter);
                        a++;
                    }
                    break;
                case AutoListView.LOAD:
                    listView.onLoadComplete();
                    if (result!=null){
                        list.addAll(result);
                    }

                    break;
            }
            if (result!=null) {
//                listView.setResultSize(result.size());
            }else{
//                listView.setResultSize(0);
            }
            adapter.notifyDataSetChanged();

        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jycoord_visit);
        listView = (AutoListView) findViewById(R.id.coor_visit_alv);
        img_cx= (ImageView) findViewById(R.id.item_image_cx);
        img_back= (ImageView) findViewById(R.id.detailed_doc_img_back);
        img_lr= (ImageView) findViewById(R.id.item_image_lr);
        img_me= (ImageView) findViewById(R.id.item_image_w);
        img_tj= (ImageView) findViewById(R.id.item_image_tj);
        listView.setDivider(new ColorDrawable(Color.rgb(216, 216, 216)));
        listView.setDividerHeight(1);
        listView.setOnRefreshListener(this);
        listView.setOnLoadListener(this);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
//                    if (getIntent().getIntExtra("KXA01",0)!=0){
//                        object.put("KXA01",getIntent().getIntExtra("KXA01",0));
//                    }
                        object.put("KPA0601",getIntent().getStringExtra("KPA0601"));
                        object.put("KPA0602",getIntent().getStringExtra("KPA0602"));
                        object.put("CTS0101",getIntent().getStringExtra("CTS0101"));
                        object.put("CTS0102",getIntent().getStringExtra("CTS0102"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                String result = NetWorkUtils.getCount(baseClass.find_coor_bf_URL+1,json);
                list = VisitJson.jsonVisitMessage(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(list!=null){
                            adapter = new MyAdapter();
                            listView.setAdapter(adapter);
                            a++;
                            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    GetVisit.create();
                                    GetVisit.scheduleEntity = list.get(position-1);
                                    Intent intent = new Intent(JYCoordVisitActivity.this, detailedVisitActivity.class);
                                    intent.putExtra("TAG","coor");
                                    intent.putExtra("JY","JY");
                                    startActivity(intent);
                                }
                            });
                        }else {
                            listView.setVisibility(View.GONE);
                            Toast.makeText(JYCoordVisitActivity.this, "该条件下未搜索到信息", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }).start();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        img_tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoordVisitActivity.this, JDcoorActivity.class);
                intent.putExtra("tt","tt");
                startActivity(intent);
                finish();
            }
        });
        img_me.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoordVisitActivity.this, JDcoorActivity.class);
                intent.putExtra("me","me");
                startActivity(intent);
                finish();
            }
        });
        img_lr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoordVisitActivity.this, JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(JYCoordVisitActivity.this, JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
    }

    private void loadData(int refresh) {
        if (refresh== AutoListView.REFRESH){
           UpdateTextTask updateTextTask = new UpdateTextTask();
            updateTextTask.execute();
        }else if(refresh== AutoListView.LOAD){
           LoadTask loadTask = new LoadTask();
            loadTask.execute();
        }

    }
    class LoadTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 1;
            msg.what = what;
            page=page+1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
//                  object.put("KXA01",getIntent().getIntExtra("KXA01",0));
                    object.put("KPA0601",getIntent().getStringExtra("KPA0601"));
                    object.put("KPA0602",getIntent().getStringExtra("KPA0602"));
                    object.put("CTS0101",getIntent().getStringExtra("CTS0101"));
                    object.put("CTS0102",getIntent().getStringExtra("CTS0102"));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_coor_bf_URL+page,json);
            alist = VisitJson.jsonVisitMessage(result);
            return null;
        }
        @Override
        protected void onPostExecute(Integer integer) {
            if(alist!=null){
                msg.obj = alist;
                handler.sendMessage(msg);
            }else {
                msg.obj = alist;
                handler.sendMessage(msg);
                Toast.makeText(JYCoordVisitActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }
    class UpdateTextTask extends AsyncTask<Void,Integer,Integer> {
        Message msg = handler.obtainMessage();
        @Override
        protected Integer doInBackground(Void... voids) {
            int what = 0;
            msg.what = what;
            page =1;
            SC sc = new SC();
            JSONObject object = sc.storage(sp);
            try {
//                    object.put("KXA01",getIntent().getIntExtra("KXA01",0));
                    object.put("KPA0601",getIntent().getStringExtra("KPA0601"));
                    object.put("KPA0602",getIntent().getStringExtra("KPA0602"));
                    object.put("CTS0101",getIntent().getStringExtra("CTS0101"));
                    object.put("CTS0102",getIntent().getStringExtra("CTS0102"));

            } catch (JSONException e) {
                e.printStackTrace();
            }
            String json = object.toString();
            String result = NetWorkUtils.getCount(baseClass.find_coor_bf_URL+1,json);
            alist = VisitJson.jsonVisitMessage(result);
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            if(alist!=null){
                if (listView.getVisibility()==View.GONE){
                    listView.setVisibility(View.VISIBLE);
                }
                msg.obj = alist;
                handler.sendMessage(msg);

            }else {
                listView.setVisibility(View.GONE);
                Toast.makeText(JYCoordVisitActivity.this, "无更多数据", Toast.LENGTH_SHORT).show();
            }
            super.onPostExecute(integer);
        }
    }

    @Override
    public void onLoad() {
        loadData(AutoListView.LOAD);
    }

    @Override
    public void onRefresh() {
        loadData(AutoListView.REFRESH);
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {

            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyAdapter.ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.layout_visit_item,null);
                holder = new MyAdapter.ViewHolder();
                holder.item_text1 = (TextView) convertView.findViewById(R.id.listview_item_txt_name);
//                holder.item_text2= (TextView) convertView.findViewById(R.id.listview_item_txt_bz);
                holder.item_text3 = (TextView) convertView.findViewById(R.id.listview_item_createTime);

                convertView.setTag(holder);
            } else {
                holder = (MyAdapter.ViewHolder) convertView.getTag();
            }
            holder.item_text1.setText(list.get(position).getSchedule_theme());
            holder.item_text3.setText(list.get(position).getCreate_time());
            return convertView;
        }
        class ViewHolder {
            TextView item_text1, item_text2, item_text3,item_text4;
        }
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        if (GetCoorVisitData.list!=null){
            list.clear();
            list.addAll(GetCoorVisitData.list);
            adapter.notifyDataSetChanged();
            GetCoorVisitData.list=null;
        }else {
            onRefresh();
        }
    }
}
